/**
 *
 * ......................我佛慈悲.......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG....................
 *         CSDN博客:http://blog.csdn.net/song5347
 *       github地址:https://github.com/lingsuiderongyan
 *             邮箱:lingsuiderongyan@163.com
 */

import UIKit

class LSUserAccount: NSObject, NSCoding {
    
    //用户授权的唯一依据
    var access_token: String?
    
    //access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0 {
        didSet {
            //过期时间 = 获取accesstoken那一刻起的时间 + 过期秒数
            //dateByAddingTimeInterval函数的意思在当前时间的基础加上多少秒
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
            
        }
    }
    
    //  过期时间
    var expiresDate: NSDate?
    
    // 授权用户的UID
    var uid: Int64 = 0
    // 用户名
    var name: String?
    // 头像
    var avatar_large: String?
    
    //kvc构造函数
    //AnyObject,表示任意对象类型
    init(dic: [String: AnyObject]) {
        
        super.init()
        setValuesForKeysWithDictionary(dic)
        
    }
    
    //防止字段不匹配,导致崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //重写description
    override var description: String {
        
        let keys = ["access_token", "expires_in", "uid", "name", "avatar_large"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeInt64(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeInt64ForKey("uid")
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    //调用该方法保存用户对象到指定沙盒路径
    func saveUserAccount() {
        //  设置保存路径
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archive")
        
        //  执行归档
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        
    }
    
    //类函数
    class func loadUserAccount() -> LSUserAccount?{
        //设置保存路径
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archive")
        
        //执行解档
        return NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? LSUserAccount
    }
    
}





















